package com.zkt.auth.application.controller;


import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.zkt.auth.application.convert.AuthRoleDTOConverter;
import com.zkt.auth.application.dto.AuthRoleDTO;
import com.zkt.auth.common.entity.Result;
import com.zkt.auth.domain.entity.AuthRoleBO;
import com.zkt.auth.domain.service.AuthRoleDomainService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/3/22 15:52
 * @description 角色相关controller层
 **/

@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {
	
	@Resource
	private AuthRoleDomainService authRoleDomainService;
	
	/**
	 * 新增角色
	 *
	 * @param authRoleDTO 角色信息
	 * @return 是否成功
	 */
	@PostMapping("add")
	public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("UserController.add.dto:{}", JSON.toJSONString(authRoleDTO));
			}
			
			if (StrUtil.isBlank(authRoleDTO.getRoleName())) {
				throw new Exception("角色名称不能为空");
			}
			if (StrUtil.isBlank(authRoleDTO.getRoleKey())) {
				throw new Exception("角色标识不能为空");
			}
			
			AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDtoToBo(authRoleDTO);
			
			return Result.ok(authRoleDomainService.add(authRoleBO));
		} catch (Exception e) {
			log.error("UserController.add.error:{}", e.getMessage(), e);
			return Result.fail("新增角色失败");
		}
	}
	
	/**
	 * 修改角色
	 *
	 * @param authRoleDTO 角色信息
	 * @return 是否成功
	 */
	@PostMapping("update")
	public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("UserController.update.dto:{}", JSON.toJSONString(authRoleDTO));
			}
			
			if (ObjUtil.isEmpty(authRoleDTO.getId())) {
				throw new Exception("角色id不能为空");
			}
			
			AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDtoToBo(authRoleDTO);
			
			return Result.ok(authRoleDomainService.update(authRoleBO));
		} catch (Exception e) {
			log.error("UserController.update.error:{}", e.getMessage(), e);
			return Result.fail("修改角色失败");
		}
	}
	
	/**
	 * 删除角色
	 *
	 * @param authRoleDTO 角色信息
	 * @return 是否成功
	 */
	@PostMapping("delete")
	public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("UserController.delete.dto:{}", JSON.toJSONString(authRoleDTO));
			}
			
			if (ObjUtil.isEmpty(authRoleDTO.getId())) {
				throw new Exception("角色id不能为空");
			}
			
			AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDtoToBo(authRoleDTO);
			
			return Result.ok(authRoleDomainService.delete(authRoleBO));
		} catch (Exception e) {
			log.error("UserController.delete.error:{}", e.getMessage(), e);
			return Result.fail("删除角色失败");
		}
	}
	
	/**
	 * 角色启用/禁用
	 *
	 * @param authRoleDTO 角色信息
	 * @return 是否成功
	 */
	@PostMapping("changeStatus")
	public Result<Boolean> changeStatus(@RequestBody AuthRoleDTO authRoleDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("UserController.changeStatus.dto:{}", JSON.toJSONString(authRoleDTO));
			}
			
			if (ObjUtil.isEmpty(authRoleDTO.getId())) {
				throw new Exception("角色id不能为空");
			}
			if (ObjUtil.isEmpty(authRoleDTO.getStatus())) {
				throw new Exception("角色状态不能为空");
			}
			
			AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDtoToBo(authRoleDTO);
			
			return Result.ok(authRoleDomainService.changeStatus(authRoleBO));
		} catch (Exception e) {
			log.error("UserController.changeStatus.error:{}", e.getMessage(), e);
			return Result.fail("角色启用/禁用失败");
		}
	}
	
}